# -*- coding: utf-8 -*-
'''
@File: form.py
@Time: 2023/5/6 16:18
@Author: sunda
'''


""" 表单数据 """
from flask_wtf import FlaskForm  # 导入FlaskForm
from wtforms.fields import simple
from wtforms import SubmitField, SelectField, StringField
from wtforms import validators,widgets
from wtforms.validators import DataRequired

#自定义验证规则
# 定义
class validate_username(object):
    '''自定义验证规则'''

    def __init__(self, message):
        self.message = message

    def __call__(self, form, field):
        print(field.data, "用户输入的信息")
        if field.data == "root":
            raise validators.ValidationError(self.message)
#登录表单
class LoginForm(FlaskForm):
    '''Form'''
    username = simple.StringField(
        label="用户名",
        widget=widgets.TextInput(),
        validators=[
            validators.DataRequired(message="用户名不能为空"),
            validators.Length(max=8, min=3, message="用户名长度必须大于%(max)d且小于%(min)d")
        ]
        #使用自定义验证规则
        # validators=[ validate_username(message='用户名不能是root'),],#自定义验证类
    )

    password = simple.PasswordField(
        label="密码",
        validators=[
            validators.DataRequired(message="密码不能为空"),
            validators.Length(max=18, min=4, message="密码长度必须大于%(max)d且小于%(min)d"),
            validators.Regexp(regex="\d+", message="密码必须是数字"),
        ]
    )
    submit = SubmitField(label='登录')  # 提交按钮submit

#日历查询表单
class CalendarForm(FlaskForm):
    '''Form'''
    year = SelectField(choices='')
    areas=[('中国内地','中国内地'),('中国香港','中国香港')]
    area = SelectField(choices=areas)
    submit = SubmitField(label='搜索')  # 提交按钮submit

''' 数据字典主表表单'''
class DictTypeFrom(FlaskForm):
    key = StringField(label="字典类型",validators=[DataRequired(message="字典类型不能为空")])
    value = StringField(label="字典类型值", validators=[DataRequired(message="字典类型值不能为空")])
    desc = StringField(label="字典类型描述")
    status = StringField(label="状态",validators=[DataRequired(message="0 / 1")])
    submit = SubmitField(label='提交')  # 提交按钮submit

''' 数据字典从表表单'''
class DictItemFrom(FlaskForm):
    sort_id = StringField(label="排序", validators=[DataRequired(message="字典值不能为空")])
    key = StringField(label="字典key",validators=[DataRequired(message="字典key不能为空")])
    value = StringField(label="字典值", validators=[DataRequired(message="字典值不能为空")])
    desc = StringField(label="字典描述", validators=[DataRequired(message="字典值不能为空")])
    status = StringField(label="状态",validators=[DataRequired(message="0 / 1")])
    submit = SubmitField(label='提交')  # 提交按钮submit